package com.person.webdemoproject2.service;

import com.person.webdemoproject2.mapper.DeleteMapper;
import com.person.webdemoproject2.utils.jwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeleteServiceimpl implements DeleteService {
    @Autowired
    DeleteMapper deleteMapper;

    @Override
    public boolean deletePost(Integer postid, String token) {
        Integer userId = jwtUtils.getUserId(token);
        Integer postId = deleteMapper.getPostId(userId, postid);
        if (postId.equals(postid)) {
            deleteMapper.deletePost(postId);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteComment(String token, Integer commentId) {
        Integer userId = jwtUtils.getUserId(token);
        Integer CommentId=deleteMapper.getCommentId(userId,commentId);
        if (CommentId.equals(commentId)) {
            deleteMapper.deleteComment(CommentId);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(String token, String password) {
        Integer userId = jwtUtils.getUserId(token);
        String truePassword= deleteMapper.getPassword(userId);
        if (truePassword.equals(password)) {
            deleteMapper.deleteUser(userId);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteCollection(String token, Integer postid) {
        Integer userId = jwtUtils.getUserId(token);
        Integer collectId = deleteMapper.getCollect(userId,postid);
        if (collectId!=null) {
            deleteMapper.deleteCollection(collectId);
            return true;
        }
        return false;
    }


}
